<?php
namespace common\queue;

use api\modules\v1\models\shop\GoodsInfo;
use common\models\CacheKeys;
use common\models\circle\Circle;
use common\models\friend\Friend;
use common\models\friend\FriendApply;
use common\models\group\Group;
use common\models\group\GroupUser;
use common\models\im\Im;
use common\models\Status;
use common\models\user\UserBusiness;
use yii\base\Exception;
use yii\queue\Job;
use Yii;

class User extends \common\models\user\User implements Job {

    public $data = [];
    public function __construct($args)
    {
        parent::__construct();
        $this->data = $args;
    }

    public function execute($queue)
    {
        $user = $this->data['user'];
        $userId = $this->data['user_id'];
        $trans = static::getDb()->beginTransaction();
        try {
//            $db = static::getDb();
            // 删除好友关系(双向删除) 与 申请记录
            $whereFriend = $whereAndFriend = [];
            $whereAndFriend[] = [
                'or',
                ['user_id' => $userId],
                ['friend_id' => $userId],
            ];
            $friendModel = new Friend();
            $friendList = $friendModel::getByAndWhere($whereFriend,$whereAndFriend,['user_id','user_tx_code','friend_id','friend_tx_code']);
            if ($friendList) {
                foreach ($friendList as $key => $value) {
                    $friendModel->delFriend($value['user_id'],$value['friend_id'],$value['user_tx_code'],$value['friend_tx_code']);
                }
            }


            // 删除群主的群 解散群
            $whereGroup = $whereAndGroup = [];
            $groupModel = new Group();
            $whereGroup['user_id'] = $userId;
            $friendApplyModel = new FriendApply();
            $groupList = $groupModel::getByAndWhere($whereGroup,$whereAndGroup,['id','tx_code']);
            if ($groupList) {
                foreach ($groupList as $key => $value) {
                    $groupModel->delGroup($userId,$value['id'],$value['tx_code']);
                    // 发送未读通知
                    $friendApplyModel::notReadApplyNum($userId, $value['id']);
                }
            }

            // 删除群成员 用户主动退群
            $whereGroupUser = $whereAndGroupUser = [];
            $groupUserModel = new GroupUser();
            $whereGroupUser['user_id'] = $userId;
            $groupList = $groupUserModel::getByAndWhere($whereGroupUser,$whereAndGroupUser,["id",'user_id','user_tx_code','user_nick_name','group_id','group_tx_code','user_type']);
            if ($groupList) {
                foreach ($groupList as $key => $value) {
                    if ($value['user_id'] == $userId && $value['user_type'] == Status::GROUP_USER_TYPE_HOME) {
                        continue;
                    }
                    $groupUserModel->quitGroupUser($value['id'],$userId,$user['tx_code'], $user['nick_name'],$value['group_id'],$value['group_tx_code']);
                }
            }


            // 删除橱窗信息
            UserBusiness::updateData(['status' => 3],['user_id' => $user['id']]);
            GoodsInfo::updateData(['status' => 2],['user_id' => $user['id']]);
            // 删除朋友圈信息
            Circle::updateData(['status' => 4],['user_id' => $user['id']]);

            // 删除用户信息
//            $resGroupApplyDel = static::deleteAll([
//                'id' => $userId,
//            ]);
//            if ($resGroupApplyDel === false) {
//                throw new Exception('注销失败！');
//            }

            $time = date("Y/m/d H:i");

            $up['login_name'] = "[注销{$time}]" . $user['login_name'];
            $up['update_time'] = $time;
            $up['is_delete'] = Status::COMMON_RADIO_NO;
            $up['status'] = Status::COMMON_RADIO_NO;
            $up['phone'] = null;
            $up['access_token'] = null;
            static::updateData($up,['id' => $user['id']]);

            # 强制T下线
            $res = (new Im()) ->loginOut($user['tx_code']);
            if (!$res) {
                throw new Exception('禁用失败');
            }

            $trans->commit();
        } catch (Exception $e) {
            $trans->rollBack();
        }
    }


}